package com.sx.pipeline.ui.task.participate.detail

import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.lxj.xpopup.XPopup
import com.sx.mvvmlibs.base.EasyAdapter
import com.sx.mvvmlibs.view.SpaceItemDecoration
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.DisclosureTypeAdapter
import com.sx.pipeline.adapter.ReleaseImageAdapter
import com.sx.pipeline.adapter.TaskTypeDetailAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IdentityType
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.app.config.ReleaseTaskType
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActReleaseDetailBinding
import com.sx.pipeline.repos.http.res.TakePartBean
import com.sx.pipeline.ui.task.ReleaseTaskVM


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 我参与的任务-任务详情
 */
@Route(path = RoutePath.Mine.PARTICIPATE_TASK_DETAIL)
class ParticipateTaskDetailActivity :
    CYDataBindingBaseActivity<ActReleaseDetailBinding, ReleaseTaskVM>(R.layout.act_release_detail),
    View.OnClickListener {
    @JvmField
    @Autowired(name = IntentKey.ID)
    var taskId: Int? = 0

    private var mAdapter: TaskTypeDetailAdapter? = null
    private var mAdapterCondition: TaskTypeDetailAdapter? = null
    private var mAdapterExplorationPhotos: ReleaseImageAdapter? = null //现场勘探照片适配器
    private var mAdapterConstructionDrawings: ReleaseImageAdapter? = null//施工图纸照片适配器
    private var mAdapterApprovalProcedures: ReleaseImageAdapter? = null //审批手续照片适配器
    private var mAdapterConfessionPhotos: ReleaseImageAdapter? = null //交底照片适配器
    private var mAdapterDisclosure: DisclosureTypeAdapter? = null //交底单位类型适配器

    override fun initData() {
        super.initData()
        mBinding.sbAddress.rightView.setCompoundDrawablesWithIntrinsicBounds(getDrawable(R.drawable.ic_mine_location), null, null, null)
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        //请求任务详情
        taskId?.let { mViewModel.getTaskDetail(it) }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.taskDetailDataEvent) {
            //任务状态
            when (it?.status) {
                ReleaseTaskType.PENDING_REVIEW.code -> mBinding.tvStatus.setText(R.string.txt_pending_review)
                ReleaseTaskType.AGENCY.code -> {
                    mBinding.tvStatus.setText(R.string.txt_upcoming)
                    //显示 结束任务
                    mBinding.tvAction.text = getString(R.string.txt_processing_tasks)
                    mBinding.tvAction.visibility = View.VISIBLE

                    //显示任务通过时间
                    mBinding.sbPassTime.rightText = it?.throughTime
                    mBinding.sbPassTime.visibility = View.VISIBLE
                }
                ReleaseTaskType.IN_PROGRESS.code -> {
                    mBinding.tvStatus.setText(R.string.txt_in_progress)

                    mBinding.tvAction.visibility = View.GONE
                }
                ReleaseTaskType.COMMENT.code -> {
                    mBinding.tvStatus.setText(R.string.txt_be_evaluated)
                    mBinding.tvAction.visibility = View.GONE
                }
                ReleaseTaskType.COMPLETED.code -> {
                    mBinding.tvStatus.setText(R.string.txt_completed)
                    if (it.evaluate) {
                        //显示  查看评价
                        mBinding.tvAction.text = getString(R.string.txt_view_reviews)
                        mBinding.tvAction.visibility = View.VISIBLE
                    }

                }
                ReleaseTaskType.OVER.code -> {
                    mBinding.tvStatus.setText(R.string.txt_over)
                }
            }
            //发起单位
            mBinding.sbInitiatingUnit.rightText = it?.unitName
            //任务标题
            mBinding.sbTitle.rightText = it?.title
            //任务描述
            mBinding.sbDesc.rightText = it?.taskDescription
            //项目名称
            mBinding.sbProjectName.rightText = it?.name
            //工作量
            mBinding.sbWorkload.rightText = it?.workload
            //计量单位
            mBinding.sbUnit.rightText = it?.unit
            //施工地点
            mBinding.sbAddress.rightText = it?.addressDetail
            //任务性质
            mBinding.sbNature.rightText = it?.nature
            //开始时间
            mBinding.sbStartTime.rightText = it?.startTime
            //结束时间
            mBinding.sbEndTime.rightText = it?.endTime
            //负责人
            mBinding.sbContact.rightText = it?.contact
            //负责人电话
            mBinding.sbContactNumber.rightText = it?.contactPhone
            //审核人
            mBinding.sbAuditby.rightText = it?.auditBy

            //任务类型
            it?.taskType?.split(',')?.let { it1 -> initTaskType(it1) }
            //任务发布现场初步勘探情况
            it?.taskCondition?.split(',')?.let { it1 -> initTaskCondition(it1) }
            //施工现场勘查
            it?.livePictures?.split(",")?.let { it1 ->
                if (!TextUtils.isEmpty(it?.livePictures)) {
                    initExplorationPhotos(it1)
                    mBinding.tabExplorationPhotos.visibility = View.VISIBLE
                }
            }
            //施工图纸
            it?.constructionDrawing?.split(",")?.let { it1 ->
                if (!TextUtils.isEmpty(it?.constructionDrawing)) {
                    initConstructionDrawings(it1)
                    mBinding.tabConstructionDrawings.visibility = View.VISIBLE
                }
            }
            //市政审批手续
            it?.formalities?.split(",")?.let { it1 ->
                if (!TextUtils.isEmpty(it?.formalities)) {
                    initApprovalProcedures(it1)
                    mBinding.tabApprovalProcedures.visibility = View.VISIBLE
                }
            }

            //任务新建人
            mBinding.sbCreateby.rightText = it?.createBy
            //任务发布时间
            mBinding.sbCreateTime.rightText = it?.createTime
            //通过时间
            if (it?.throughTime != null) {
                mBinding.sbPassTime.rightText = it?.throughTime
                mBinding.sbPassTime.visibility = View.VISIBLE
            }
            //取消时间
            if (it?.cancelTime != null) {
                mBinding.sbCancelledTime.rightText = it?.cancelTime
                mBinding.sbCancelledTime.visibility = View.VISIBLE
            }
            //结束时间
            if (it?.cancelTime != null) {
                mBinding.sbFinishTime.rightText = it?.cancelTime
                mBinding.sbFinishTime.visibility = View.VISIBLE
            }
            //交底列表
            it?.takePartList?.let { it1 -> initDiscolosureUnit(it1) }
        }


        observe(mViewModel.successTaskDelayEvent) {
            if (it == true) {
                taskId?.let { it1 -> mViewModel.getTaskDetail(it1) }
            }
        }
    }

    //任务类型
    private fun initTaskType(data: List<String>) {
        mAdapter = TaskTypeDetailAdapter(this, data)
        mBinding!!.rvTaskType.layoutManager =
            GridLayoutManager(this, 3)
        mBinding!!.rvTaskType.addItemDecoration(
            SpaceItemDecoration(15)
        )
        mAdapter?.maxSelectedCount = 0
        mBinding.rvTaskType.setAdapter(mAdapter)
        mAdapter?.setSelectMode(EasyAdapter.SelectMode.MULTI_SELECT)
    }

    private fun initTaskCondition(data: List<String>) {
        mAdapterCondition = TaskTypeDetailAdapter(this, data)
        mBinding!!.rvPreliminarySurvey.layoutManager =
            GridLayoutManager(this, 3)
        mBinding!!.rvPreliminarySurvey.addItemDecoration(
            SpaceItemDecoration(15)
        )
        mAdapter?.maxSelectedCount = 0
        mBinding.rvPreliminarySurvey.setAdapter(mAdapterCondition)
        mAdapterCondition?.setSelectMode(EasyAdapter.SelectMode.MULTI_SELECT)
    }

    //施工现场勘查
    fun initExplorationPhotos(data: List<String>) {
        mAdapterExplorationPhotos = ReleaseImageAdapter(this, data)
        mBinding.rvExplorationPhotos.layoutManager =
            GridLayoutManager(this, 3)
        mBinding.rvExplorationPhotos.setAdapter(mAdapterExplorationPhotos)
        mAdapterExplorationPhotos?.setSelectMode(EasyAdapter.SelectMode.SINGLE_SELECT)
    }

    //施工图纸
    fun initConstructionDrawings(data: List<String>) {
        mAdapterConstructionDrawings = ReleaseImageAdapter(this, data)
        mBinding.rvConstructionDrawings.layoutManager =
            GridLayoutManager(this, 3)
        mBinding.rvConstructionDrawings.adapter = mAdapterConstructionDrawings
        mAdapterConstructionDrawings?.selectMode = EasyAdapter.SelectMode.SINGLE_SELECT
    }

    //审批手续
    fun initApprovalProcedures(data: List<String>) {
        mAdapterApprovalProcedures = ReleaseImageAdapter(this, data)
        mBinding.rvApprovalProcedures.layoutManager =
            GridLayoutManager(this, 3)
        mBinding.rvApprovalProcedures.adapter = mAdapterApprovalProcedures
        mAdapterApprovalProcedures?.selectMode = EasyAdapter.SelectMode.SINGLE_SELECT
    }

    //交底单位信息
    fun initDiscolosureUnit(data: List<TakePartBean>) {
        mAdapterDisclosure = DisclosureTypeAdapter(this, data)
        mBinding!!.rvDisclosure.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        mBinding!!.rvDisclosure.addItemDecoration(
            SpaceItemDecoration(28)
        )
        mBinding.rvDisclosure.setAdapter(mAdapterDisclosure)
        mAdapterDisclosure?.setSelectMode(EasyAdapter.SelectMode.SINGLE_SELECT)
        mAdapterDisclosure?.setSelected(0)
        //监听多选
        mAdapterDisclosure?.setOnItemSingleSelectListener { itemPosition, isSelected ->
            //点击切换交底单位信息
            mBinding.sbWorkUnit.rightText = data[itemPosition].unitName
            mBinding.sbWorkerName.rightText = data[itemPosition].contact
            //交底内容
            mBinding.tvWorkContent.text = data[itemPosition].contentDescribe
            //交底照片
            data[itemPosition]?.imageUrls?.split(",")?.let { it1 ->
                initConfessionPhotos(it1)
                mBinding.tabConfessionPhotos.visibility = View.VISIBLE
            }
        }

        if (data.isNotEmpty()) {
            //交底施工单位
            mBinding.sbWorkUnit.rightText = data[0].unitName
            mBinding.sbWorkerName.rightText = data[0].contact
            when (mViewModel.mModel.getUserData()?.identity) {
                //运营单位显示内容
                IdentityType.OPERATING_UNIT.code -> {
                    mBinding.sbWorkUnit.leftText =
                        getString(R.string.txt_participate_in_disclosure_unit)
                    mBinding.sbWorkerName.leftText = getString(R.string.txt_operator)
                    //交底内容
                    mBinding.tvWorkContent.text = data[0].contentDescribe
                    //交底照片
                    data[0]?.imageUrls?.split(",")?.let { it1 ->
                        initConfessionPhotos(it1)
                        mBinding.tabConfessionPhotos.visibility = View.VISIBLE
                    }
                    mBinding.tabConfessionContent.visibility = View.VISIBLE
                }
                //施工单位显示内容
                IdentityType.BUILD_UNIT.code -> {
//                    mBinding.sbWorkUnit.leftText=getString(R.string.txt_participate_in_construction_disclosure_units)
//                    mBinding.sbWorkerName.leftText=getString(R.string.txt_construction_workers)
//                    //环境描述
//                    mBinding.tvMilieuDescribe.text=data[0].milieuDescribe
//                    //配套服务需求
//                    mBinding.tvNeedContent.text=data[0].needNeed
//                    mBinding.tabMilieuDescribe.visibility=View.VISIBLE
//                    mBinding.tabNeedContent.visibility=View.VISIBLE
                }
            }
            mBinding.tabUnit.visibility = View.VISIBLE
        }

    }

    //交底照片
    fun initConfessionPhotos(data: List<String>) {
        mAdapterConfessionPhotos = ReleaseImageAdapter(this, data)
        mBinding.rvConfessionPhotos.layoutManager =
            GridLayoutManager(this, 3)
        mBinding.rvConfessionPhotos.adapter = mAdapterConfessionPhotos
        mAdapterConfessionPhotos?.selectMode = EasyAdapter.SelectMode.SINGLE_SELECT
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            //处理任务
            R.id.tv_action -> {
                when (mViewModel.taskDetailDataEvent.value?.status) {
                    ReleaseTaskType.AGENCY.code -> {
                        //跳转处理任务
                        taskId?.let {
                            RouteManager.Mine.intentToTaskProcess(
                                it,
                                mViewModel.taskDetailDataEvent.value?.hasGroup.toString()
                            )
                        }
                    }
                    ReleaseTaskType.COMPLETED.code -> {
                        //查看评价
                        taskId?.let { RouteManager.Mine.intentToEvaluationList(it) }
                    }

                }
            }
            R.id.sb_address -> {
                ARouter.getInstance().build(RoutePath.Main.SHOW_LOCATION)
                    .withString("lat", mViewModel.taskDetailDataEvent.value?.latitude)
                    .withString("lon",mViewModel.taskDetailDataEvent.value?.longitude)
                    .withString("address",mViewModel.taskDetailDataEvent.value?.addressDetail)
                    .navigation()
            }

        }
    }


}